package com.cljh.controller;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.cljh.bean.Distinct;
import com.cljh.config.AuthAccess;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.controller.page.Result;
import com.cljh.service.DistinctService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/distincts")
public class DistinctController {
    @Autowired
    DistinctService distinctService;

    @AuthAccess
    @PostMapping("/{pn}/{size}")
    public Result selectProduct(@PathVariable(value = "pn") Integer pn, @PathVariable(value = "size")Integer size, @RequestBody Distinct distinct){
        PageBean<Distinct> distinctPageBean = distinctService.selectAllByCondition(pn, size, distinct);


        Integer code=distinctPageBean !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=distinctPageBean!=null ? "查询到信息":"未查询到信息";
        return new Result(code,distinctPageBean,msg);
    }

    @PostMapping
    public Result save(@RequestBody Distinct distinct){
        boolean save = distinctService.save(distinct);

        Integer code=save ? Code.SAVE_OK:Code.SAVE_ERR;
        String msg=save ? "添加成功":"添加失败";
        return new Result(code,save,msg);
    }

    /**
     * id删除
     * @param id
     * @return Result
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean delete = distinctService.delete(id);

        Integer code=delete ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=delete ? "删除一个成功":"删除一个失败";
        return new Result(code,delete,msg);
    }

    /**
     * 更新
     * @param distinct 商品类
     * @return Result
     */
    @PutMapping
    public Result update(@RequestBody Distinct distinct){
        boolean update = distinctService.update(distinct);

        Integer code=update ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=update ? "修改成功":"修改失败";
        return new Result(code,update,msg);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteIds")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        boolean b = distinctService.deleteByIds(ids);

        Integer code=b ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=b ? "删除成功":"删除失败";
        return new Result(code,b,msg);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Distinct distinct = distinctService.selectById(id);
        Integer code=distinct !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=distinct!=null ? "查询到信息":"未查询到信息";
        return new Result(code,distinct,msg);
    }

    @GetMapping
    public Result getAll(){

        List<Distinct> distinct = distinctService.selectAll();
        Integer code=distinct !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=distinct!=null ? "查询到信息":"未查询到信息";
        return new Result(code,distinct,msg);
    }

}
